@include('admin::layouts.header')
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>省市区管理</legend>
    </fieldset>
    <div class="demoTable layui-row">
        <div class="layui-form layui-form-pane" style="float:left;">
			<div class="layui-form-item">
				<label class="layui-form-label">快捷查询</label>
				<div class="layui-input-inline layui-form">
					<input class="layui-input" name="key" id="key" placeholder="请输入名称">
				</div>
			</div>
        </div>
		<div class="layui-form layui-form-pane" style="float:left;width:460px">
			<div class="layui-form-item">
				<label class="layui-form-label">添加时间</label>
				<div style="width:72%" class="layui-input-inline layui-form">
					<input style="width:100%" type="text" class="layui-input" id="created_at" placeholder="请选择时间">
				</div>
			</div>	
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">状态</label>
				<div class="layui-input-inline layui-form">
					<select id="status_d">
						<option value="">请选择</option>
						@foreach($status as $key=>$value)
							<option value="{{$key}}">{{$value}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
    </div>
	<div class="demoTable layui-row">
		<button class="layui-btn" style="margin-left:30px;margin-top:15px;" id="search" data-type="reload">查询</button>
		<a href="" style="margin-top:15px;" class="layui-btn">重置</a>
	</div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
@include('admin::layouts.footer')
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="@{{d.id}}" lay-skin="switch" lay-text="{!! config('admin.enabled') !!}|{!! config('admin.forbidden') !!}" lay-filter="status" @{{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="sort">
    <input name="@{{d.id}}" data-id="@{{d.id}}" class="list_order layui-input" value=" @{{d.sort}}" size="10"/>
</script>
<script type="text/html" id="action">
	@if($level_type < 3)
	 <a href="{{route('admin.area.index',['level_type'=>$level_type+1])}}&pid=@{{d.id}}" class="layui-btn layui-btn-xs layui-btn-warm">查看子级</a>
	@endif
    <a href="/admin/area/@{{d.id}}/edit" class="layui-btn layui-btn-xs">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="topBtn">
   <a href="{{route('admin.area.create',['pid'=>$pid])}}&level_type={{$level_type}}" class="layui-btn layui-btn-sm">添加</a>
   @if($level_type != 1)
    <a href="{{route('admin.area.index',['pid'=>$s_pid])}}&level_type={{$level_type-1}}" class="layui-btn layui-btn-sm">返回</a>
   @endif
   <a onclick="import_data()" class="layui-btn layui-btn-sm">导入服务器数据</a>
   <a href="{{route('admin.area.export_data')}}" class="layui-btn layui-btn-sm">导出数据</a>
</script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
			id: 'ad',
            elem: '#list',
            url: "{{route('admin.area.data',['pid'=>$pid])}}&level_type={{$level_type}}",
            method:'get',
			toolbar: '#topBtn',
			title:'省市区管理',
			page: true,
            cols: [[
				{field: 'id', align: 'center',title: '编号', width: 100,sort: true, fixed: true},
                {field: 'name', align: 'center',title: '名称', width: 200},
				{field: 'short_name', align: 'center',title: '简称', width: 150},
				{field: 'merger_name', align: 'center',title: '全称路径', width: 200},
				{field: 'pinyin', align: 'center',title: '拼音', width: 200},
				{field: 'city_code',align: 'center', title: '区号', width: 150},
				{field: 'zip_code', align: 'center',title: '邮编', width: 150},
				{field: 'lng', align: 'center',title: '经度', width: 150},
				{field: 'lat', align: 'center',title: '纬度', width: 150},
                {field: 'status',align: 'center',title: '状态', width: 150,toolbar: '#status'},
                {field: 'sort',align: 'center', title: '排序', width: 80,sort: true, templet: '#sort'},
				{field:'created_at', title: '添加时间',align: 'center', width:200,sort: true},
                {width: 300,align: 'center',title: '操作', toolbar: '#action'}
            ]],
			limit: "{!! config('admin.common_limit') !!}"
        });
		//搜索
        $('#search').on('click', function () {
			var created_at 		= $('#created_at').val();
			var status  		= $('#status_d').val();
			var key  			= $('#key').val();
            if ($.trim(created_at) === '' && $.trim(status) === '' && $.trim(key) === ''){
                layer.msg("{!! config('admin.query_condition') !!}", {icon: 0});
                return;
            }
			tableIn.reload({
				where: {created_at:created_at,status:status,key:key}
			});
        });
        form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('/admin/area/'+id+'/status',{_method:'PUT',status:status},function (res) {
				layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
		import_data = function(){
            loading = layer.load(1, {shade: [0.1,'#fff']});
            $.get("{{route('admin.area.import_data')}}",function (res) {
				layer.close(loading);
				if (res.status == 1) {  
					layer.msg(res.msg,{time:1000,icon:1},function(){
						tableIn.reload();
					});
				}else{
				   layer.msg(res.error,{time:1000,icon:2});
				}
            })
        };
		$('body').on('blur','.list_order',function() {
		   loading =layer.load(1, {shade: [0.1,'#fff']}); 
           var id = $(this).attr('data-id');
           var sort = $(this).val();
           $.post('/admin/area/'+id+'/sorts',{_method:'PUT',sort:sort},function(res){
                layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
           })
        });
    });
</script>
@include('admin::layouts.delete_ajax',['url'=>route('admin.area.destroy')])
</body>
</html>